//package com.sqx.modules.utils.typeHandler;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.sqx.modules.recovery.entity.GoodsSpecifications;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.type.BaseTypeHandler;
//import org.apache.ibatis.type.JdbcType;
//import org.apache.ibatis.type.TypeHandler;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.annotation.MapperScan;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//
//import javax.sql.DataSource;
//import java.sql.CallableStatement;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * @description：TODO
// * @author： qiao
// * @create： 2024/10/15 15:41
// */
//@Configuration
//public class MyBatisConfig {
//    @Bean
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
//        factoryBean.setDataSource(dataSource);
//
//        // 注册自定义类型处理器
////        Map<Class<? extends TypeHandler<?>>, TypeHandler<?>> typeHandlers = new HashMap<>();
////        typeHandlers.put(List.class, new AttributeValueListTypeHandler());
////        factoryBean.setTypeHandlers(typeHandlers);
//
//        // 设置 mapper 文件的位置
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
//
//        // 设置别名
//        factoryBean.setTypeAliasesPackage("com.sqx.modules.recovery.entity");
//
//        return factoryBean.getObject();
//    }
//
//    @Bean
//    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }
//
//    // 自定义类型处理器
//    public class AttributeValueListTypeHandler extends BaseTypeHandler<List<GoodsSpecifications.AttributeValue>> {
//
//        @Override
//        public void setNonNullParameter(PreparedStatement ps, int i, List<GoodsSpecifications.AttributeValue> parameter, JdbcType jdbcType) throws SQLException {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                String json = objectMapper.writeValueAsString(parameter);
//                ps.setString(i, json);
//            } catch (Exception e) {
//                throw new SQLException("Error setting parameter: " + e.getMessage(), e);
//            }
//        }
//
//        @Override
//        public List<GoodsSpecifications.AttributeValue> getNullableResult(ResultSet rs, String columnName) throws SQLException {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                String json = rs.getString(columnName);
//                return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, GoodsSpecifications.AttributeValue.class));
//            } catch (Exception e) {
//                throw new SQLException("Error getting result: " + e.getMessage(), e);
//            }
//        }
//
//        @Override
//        public List<GoodsSpecifications.AttributeValue> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                String json = rs.getString(columnIndex);
//                return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, GoodsSpecifications.AttributeValue.class));
//            } catch (Exception e) {
//                throw new SQLException("Error getting result: " + e.getMessage(), e);
//            }
//        }
//
//        @Override
//        public List<GoodsSpecifications.AttributeValue> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
//            ObjectMapper objectMapper = new ObjectMapper();
//            try {
//                String json = cs.getString(columnIndex);
//                return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, GoodsSpecifications.AttributeValue.class));
//            } catch (Exception e) {
//                throw new SQLException("Error getting result: " + e.getMessage(), e);
//            }
//        }
//    }
//}
